<?php

namespace app\shop\service;

use app\common\model\info\Info as InfoModel;
use app\common\model\ad\Ad as AdModel;
use app\common\model\link\Link as LinkModel;
use think\facade\Db;
use app\common\model\settings\Setting as SettingModel;
use app\common\model\menu\Menu as MenuModel;

/**
 * 后台模型
 */
class ShopService
{

    /**
     * 构造方法
     */
    public function __construct()
    {

    }

    //获取后台首页数据
    public function getBaseData()
    {
        //文章数量
        $infoCount = (new InfoModel)->where('is_delete', '=', 0)->count();
        //广告数量
        $adCount = (new AdModel)->count();
        //友情链接数量
        $linkCount = (new LinkModel)->count();
        //栏目数量
        $menuCount = (new MenuModel)->where('is_delete', '=', 0)->count();
        //系统信息
        $sysInfo = $this->getSysInfo();
        $data['infoCount'] = [
            'infoCount' => $infoCount,
            'adCount' => $adCount,
            'linkCount' => $linkCount,
            'menuCount' => $menuCount
        ];
        $data['sysInfo'] = $sysInfo;
        return $data;

    }

    /**
     * 服务器信息
     */
    private function getSysInfo()
    {
        $sys_info['os'] = PHP_OS;
        $sys_info['zlib'] = function_exists('gzclose') ? 'YES' : '<font color="red">NO（请开启 php.ini 中的php-zlib扩展）</font>';//zlib
        $sys_info['safe_mode'] = (boolean)ini_get('safe_mode') ? 'YES' : 'NO';//safe_mode = Off
        $sys_info['timezone'] = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
        $sys_info['curl'] = function_exists('curl_init') ? 'YES' : '<font color="red">NO（请开启 php.ini 中的php-curl扩展）</font>';
        $web_server = $_SERVER['SERVER_SOFTWARE'];
        if (stristr($web_server, 'apache')) {
            $web_server = 'apache';
        } else if (stristr($web_server, 'nginx')) {
            $web_server = 'nginx';
        } else if (stristr($web_server, 'iis')) {
            $web_server = 'iis';
        }
        $sys_info['web_server'] = $web_server;
        $sys_info['phpv'] = phpversion();
        $sys_info['ip'] = $this->serverIP();
        $sys_info['postsize'] = @ini_get('file_uploads') ? ini_get('post_max_size') : '未知';
        $sys_info['fileupload'] = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : '未开启';
        $sys_info['max_ex_time'] = @ini_get("max_execution_time") . 's'; //脚本最大执行时间
        $sys_info['set_time_limit'] = function_exists("set_time_limit") ? true : false;
        $sys_info['domain'] = $_SERVER['HTTP_HOST'];
        $sys_info['memory_limit'] = ini_get('memory_limit');
        $mysqlinfo = Db::query("SELECT VERSION() as version");
        $sys_info['mysql_version'] = $mysqlinfo[0]['version'];
        if (function_exists("gd_info")) {
            $gd = gd_info();
            $sys_info['gdinfo'] = $gd['GD Version'];
        } else {
            $sys_info['gdinfo'] = "未知";
        }
        if (extension_loaded('zip')) {
            $sys_info['zip'] = "YES";
        } else {
            $sys_info['zip'] = '<font color="red">NO（请开启 php.ini 中的php-zip扩展）</font>';
        }
        $sys_info['web_name'] = SettingModel::getItem('store')['web_name'];
        $sys_info['version'] = get_version();
        return $sys_info;
    }

    private function serverIP()
    {
        // 会因为解析问题导致后台卡
        if (!empty($_SERVER['SERVER_ADDR']) && !in_array($_SERVER['SERVER_ADDR'], ['127.0.0.1'])) {
            $serviceIp = $_SERVER['SERVER_ADDR'];
        } else {
            $serviceIp = @gethostbyname($_SERVER["SERVER_NAME"]);
        }
        return $serviceIp;
    }

}